import {
  txList,
  getTxCount
} from '../../config/apiServe/new-rpc'
export default {
  data() {
    return {
      loading: false,
      tab: 0,
      txType: 0,
      Trades: [],
      params: [{
        match_one: [{
            key: "from",
            value: this.$route.query.address
          },
          {
            key: "to",
            value: this.$route.query.address
          }
        ],
        match: [],
        "sort": [{
          "key": "height",
          "ascending": false
        }],
        page: {
          number: 1,
          size: 10
        }
      }],
      txCountParams: [{
        match_one: [{
            value: "",
            key: "from"
          },
          {
            value: "",
            key: "to"
          }
        ],
        match: []
      }],
      pages: {
        number: 1,
        size: 10,
        total: 10
      },
      showTotal: 0
    }
  },
  watch: {
    tab(val) {
      this.pages.number = this.params[0].page.number = 1
      this.txType = 0
      this.params[0].match = this.txCountParams[0].match = []
      this.getTrades(val)
      this.getTradesCount(val)
    },
    $route() {
      this.tab = 0
      this.Trades = []
      this.pages = {
        number: 1,
        size: 10,
        total: 10
      }
      this.params = [{
          match_one: [{
              key: "from",
              value: this.$route.query.address
            },
            {
              key: "to",
              value: this.$route.query.address
            }
          ],
          match: [],
          "sort": [{
            "key": "height",
            "ascending": false
          }],
          page: {
            number: 1,
            size: 10
          }
        }],
        this.getTrades(this.tab)
      this.getTradesCount(this.tab)
    }
  },
  computed: {
    addr() {
      return this.$route.query.address
    },
    options() {
      return [{
          label: this.$t('address.allTx'),
          value: 0
        },
        {
          label: this.$t('address.succTx'),
          value: 1
        },
        {
          label: this.$t('address.failTx'),
          value: 2
        },
        {
          label: this.$t('address.sendTx'),
          value: 3
        },
        {
          label: this.$t('address.receTx'),
          value: 4
        }
      ]
    }
  },
  mounted() {
    this.getTrades(this.tab)
    this.getTradesCount(this.tab)
  },
  methods: {
    // 筛选交易
    filterTrade(type) {
      let match = []
      let match_one = []
      switch (type) {
        case 0:
          match = []
          match_one = [{
            key: "from",
            value: this.addr
          }, {
            key: "to",
            value: this.addr
          }]
          break;
        case 1:
          match = [{
            "query": {
              "match_one": [{
                  "key": "is_para",
                  "value": true
                },
                {
                  "key": "success",
                  "value": true
                }
              ]
            }
          }]
          match_one = [{
            key: "from",
            value: this.addr
          }, {
            key: "to",
            value: this.addr
          }]
          break;
        case 2:
          match = [{
            key: "success",
            value: false
          }, {
            key: "is_para",
            value: false
          }]
          match_one = [{
            key: "from",
            value: this.addr
          }, {
            key: "to",
            value: this.addr
          }]
          break;
        case 3:
          match = []
          match_one = [{
            key: "from",
            value: this.addr
          }]
          break;
        case 4:
          match = []
          match_one = [{
            key: "to",
            value: this.addr
          }]
          break
        default:
          break;
      }
      this.pages.number = this.params[0].page.number = 1;
      this.params[0].match = match
      this.txCountParams[0].match = match
      this.getTrades(this.tab, match_one)
      this.getTradesCount(this.tab, match_one)
    },
    getTrades(tab, match_one) {
      this.loading = true
      if (match_one) this.params[0].match_one = match_one
      if (tab === 0) {
        this.params[0].match.forEach((item, index) => {
          if (item.key === 'execer') {
            this.params[0].match.splice(index, 1)
          }
        })
      } else {
        this.params[0].match.push({
          "key": "execer",
          "value": process.env.VUE_APP_DEFAULT_EXECER
        })
      }
      txList(this.params).then(res => {
        if (res.error === null) {
          this.Trades = res.result
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    getTradesCount(tab, match_one) {
      if (match_one) this.txCountParams[0].match_one = match_one
      else {

        this.txCountParams[0].match_one[0] = {
          key: "from",
          value: this.addr
        }
        this.txCountParams[0].match_one[1] = {
          key: "to",
          value: this.addr
        }
      }
      if (tab === 0) {
        this.txCountParams[0].match.forEach((item, index) => {
          if (item.key === 'execer') {
            this.txCountParams[0].match.splice(index, 1)
          }
        })
      }
      getTxCount(this.txCountParams).then(res => {
        if (res.error === null) {
          this.showTotal = res.result
          this.pages.total = res.result > 10000 ? 10000 : res.result
        }
      })
    },
    pageChange(page) {
      console.log(page);
      this.pages.number = this.params[0].page.number = page;
      this.getTrades(this.tab)
    },
    sizeChange(size) {
      this.pages.size = this.params[0].page.size = size;
      this.pages.number = this.params[0].page.number = 1;
      this.getTrades(this.tab)
    }
  }
}